package cn.fj.org.controller;

import cn.fj.basic.util.JsonResult;
import cn.fj.basic.util.PageList;
import cn.fj.org.domain.Employee;
import cn.fj.org.query.DepartmentQuery;
import cn.fj.org.sevice.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*部门接口*/
@RestController
@RequestMapping("/emp")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeservice;

    /*查询一条
     * :@GetMapping*/
    @GetMapping("{id}")
    @ApiOperation(value = "查询单条", notes = "如果有id就是修改否则就是添加")
    public Employee queryById(@PathVariable("id") Long id) {

        return employeeservice.findById(id);
    }

    /*查询所有*/
    @GetMapping
    @ApiOperation(value = "查询全部", notes = "插叙全部")
    public List<Employee> queryAll() {

        return employeeservice.findAll();
    }

    /*分页查询
     *
     * 页面发送轻轻，携带的参数都是json
     * @RequestBody：把请求题中的json格式参数封装成参数
     * 如果有两个@PostMapping，需要加路径区分
     * */
  @PostMapping
    public PageList<Employee> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return employeeservice.queryData(departmentQuery);

    }


    //*删除*//*
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除", notes = "如果有id就是修改否则就是添加")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            employeeservice.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.msg("操作失败");
        }


    }


    /*增加和修改*/
    @PutMapping
    @ApiOperation(value = "部门添加或修改", notes = "如果有id就是修改否则就是添加")
    public JsonResult addOrUpdate(@RequestBody Employee employee) {

        try {
            if (employee.getId()  == null) {
                employeeservice.add(employee);
            }else {
                employeeservice.update(employee);
                System.out.println(employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.msg("操作失败");
        }

    }


    //批量删除
    //*删除*//*

    @PatchMapping
    public JsonResult allDelete(@RequestBody  List<Long> ids ) {
        try {
            employeeservice.allDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.msg("操作失败");
        }


    }
}



